package com.igoal.entity;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

import org.compass.annotations.Searchable;
import org.compass.annotations.SearchableId;
import org.compass.annotations.SearchableProperty;

@Entity
@Table(name="category")
@Searchable
public class Category extends BaseObject implements Audit {
	private static final long serialVersionUID = -8256629387366185471L;
	
	private Long id;
	private String category;
	private Date createDate;
	private Date updateDate;
	private DataStatus status;
	
	private Set<GoalCategory> goals = new HashSet<GoalCategory>();
	private Set<UserCategory> users = new HashSet<UserCategory>();
	
	@Override
	public boolean equals(Object o) {
		if (this == o) {
            return true;
        }
        if (!(o instanceof Goal)) {
            return false;
        }

        final Goal goal = (Goal) o;

        return !(category != null ? !category.equals(goal.getTitle()) : goal.getTitle() != null);
	}

	@Override
	public int hashCode() {
		return this.category.hashCode();
	}

	@Override
	public String toString() {
		return this.category;
	}
	
	public Category() {}
	
	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	@SearchableId
	public Long getId() {
		return id;
	}
	
	@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.category")
	public Set<GoalCategory> getGoals() {
		return goals;
	}

	public void setGoals(Set<GoalCategory> goals) {
		this.goals = goals;
	}

	@OneToMany(fetch = FetchType.LAZY, mappedBy = "pk.category")
	public Set<UserCategory> getUsers() {
		return users;
	}

	public void setUsers(Set<UserCategory> users) {
		this.users = users;
	}

	@Column(name="category", nullable=false)
	@SearchableProperty
	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="cdate")
	public Date getCreateDate() {
		return createDate;
	}

	@Enumerated(EnumType.STRING)
	@Column(nullable=false, name="status")
	@SearchableProperty
	public DataStatus getStatus() {
		return status;
	}

	@Temporal(TemporalType.TIMESTAMP)
	@Column(nullable=false, name="mdate")
	public Date getUpdateDate() {
		return updateDate;
	}

	public void setCreateDate(Date date) {
		this.createDate = date;
	}

	public void setStatus(DataStatus dataStatus) {
		this.status = dataStatus;
	}

	public void setUpdateDate(Date date) {
		this.updateDate = date;
	}

	public void setId(Long id) {
		this.id = id;
	}

}
